package com.intellagric.service.iftmonitor.Impl.ift_scene;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftSceneTypeMapper;
import com.intellagric.pojo.IftSceneType;
import com.intellagric.pojo.IftSceneTypeExample;
import com.intellagric.service.iftmonitor.ift_scene.SceneTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/23
 * @DESC： 场景分类信息管理服务
 */
@Service
public class SceneTypeServiceImpl implements SceneTypeService {

    @Autowired
    private IftSceneTypeMapper sceneTypeMapper;

    /**
     * 添加场景分类信息
     * @param sceneType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addSceneType(IftSceneType sceneType) {
        if (sceneTypeMapper.insertSelective(sceneType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除场景分类信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteSceneType(String id) {
        if (sceneTypeMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 编辑场景分类信息
     * @param sceneType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editSceneType(IftSceneType sceneType) {
        if (sceneTypeMapper.updateByPrimaryKeySelective(sceneType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询场景分类信息
     * @param id
     * @return IftSceneType
     */
    @Override
    public IftSceneType getSceneTypeById(String id) {
        return sceneTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询场景分类信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSceneTypeList() {
        IftSceneTypeExample sceneTypeExample = new IftSceneTypeExample();
        sceneTypeExample.setOrderByClause("sort");//升序排列
        sceneTypeExample.createCriteria().andIdIsNotNull();
        List<IftSceneType> sceneTypeList = sceneTypeMapper.selectByExample(sceneTypeExample);
        return LayuiDataGridResult.success().add(sceneTypeList,sceneTypeList.size());
    }

    /**
     * 通过父id查询场景分类信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSceneTypeByPid(String parentId) {
        IftSceneTypeExample sceneTypeExample = new IftSceneTypeExample();
        /*sceneTypeExample.setOrderByClause("sort");//升序排列*/
        sceneTypeExample.createCriteria().andParentIdEqualTo(parentId);
        List<IftSceneType> sceneTypeList = sceneTypeMapper.selectByExample(sceneTypeExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(sceneTypeList);
        return result;
    }

    /**
     * 查询场景分类信息列表，并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getSceneTypeTree() {
        //获取所有分类
        List<IftSceneType> sceneTypeList = this.getSceneTypeList().getData();

        //获取顶级分类
        List<IftSceneType> rootSceneTypeList = this.getSceneTypeByPid("0").getData();

        for (IftSceneType sceneType : rootSceneTypeList) {
            //每个分类项目都设置其子分类
            sceneType.setChildren(getChild(sceneType.getId(),sceneTypeList));
        }
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(rootSceneTypeList);
        return result;
    }

    /**
     * 递归查询子分类
     * @param id
     * @param sceneTypeList
     * @return ResponseMessage
     */
    private List<IftSceneType> getChild(String id, List<IftSceneType> sceneTypeList) {
        //返回的子菜单
        List<IftSceneType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (IftSceneType sceneType : sceneTypeList) {
            if (sceneType.getParentId() != null) {
                //如果列表中的父id等于当前的分类的id，那就加入到该分类子列表中
                if (sceneType.getParentId().equals(id)) {
                    childList.add(sceneType);
                }
            }
        }

        // 把子菜单的子菜单childList再循环一遍
        for (IftSceneType sceneType : childList) {
            sceneType.setChildren(getChild(sceneType.getId(),sceneTypeList));
        }

        //退出递归的条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }


}
